package components.tree;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileFilter;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import components.JDVerticalFlowLayout;

public class DialogRecordReport extends JDialog {
	private static final long serialVersionUID = 1L;

	private static String EXTENSION = ".raw";
	
	private JList list = new JList();
	private FileListModel fileListModel;	

	private JPanel panelEast = new JPanel(new JDVerticalFlowLayout(JDVerticalFlowLayout.TOP, 4, 4, true, false));
	private JButton btnClose = new JButton("Close");
	private JButton btnDelete = new JButton("Delete");
	private JButton btnOpen = new JButton("Open");
	private JButton btnNew = new JButton("New");

	private File rootFile;
	
	public DialogRecordReport(File rootFile) {
		this.rootFile = rootFile;

		getContentPane().setLayout(new BorderLayout());
		setModal(true);
		setSize(new Dimension(400, 300));
		setLocationRelativeTo(null);

		getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
		getContentPane().add(panelEast, BorderLayout.EAST);

		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		btnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				delete();
			}
		});

		btnDelete.setEnabled(false);		
		btnOpen.setEnabled(false);

		list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				updateButtons();
			}
		});

		btnNew.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		
		btnOpen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				open();
			}
		});
		
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					open();
				}
			}
		});

		panelEast.add(btnNew);
		panelEast.add(btnOpen);		
		panelEast.add(btnDelete);
		panelEast.add(new JSeparator());
		panelEast.add(btnClose);

		updateListModel();
	}

	protected void open() {
		File file = getSelected();
		if(file == null || !file.exists()){
			JOptionPane.showMessageDialog(this, "No File selected.");
			return;
		}
	  
  }

	protected void delete() {
		
		File file = getSelected();
		if(file == null || !file.exists()){
			JOptionPane.showMessageDialog(this, "No File selected.");
			return;
		}
		
		if(JOptionPane.showConfirmDialog(this, "Confirm delete?", "Confirm", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
			
		}
		
		//file.delete();
		
		updateListModel();
		
  }
	
	private File getSelected(){
		File file = null;
		
		int index = list.getSelectedIndex();
		
		if(index != -1){
			file = fileListModel.files[index];
		}
		
		return file;
	}

	private void updateListModel() {

		File[] files = rootFile.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				return pathname.isFile() && pathname.getName().endsWith(EXTENSION);
			}
		});

		fileListModel = new FileListModel(files);

		list.setModel(fileListModel);
		
		updateButtons();
	}

	public class FileListModel extends AbstractListModel {
		private static final long serialVersionUID = 1L;

		private File[] files;

		private FileListModel(File[] files) {
			this.files = files;
		}

		@Override
		public int getSize() {
			return files.length;
		}

		@Override
		public Object getElementAt(int index) {
			return files[index].getName();
		}
	}

	protected void updateButtons() {

		boolean enabled = list.getSelectedIndex() != -1;

		btnDelete.setEnabled(enabled);
		btnOpen.setEnabled(enabled);

	}

	public static void main(String[] args) {
		DialogRecordReport frame = new DialogRecordReport(new File("C:/temp"));

		frame.setVisible(true);

		System.exit(0);
	}
}
